﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Web.Security;

using Telerik.Security;
using System.Web.Profile;

namespace Telerik.Sitefinity.Services.Migration
{
	public partial class MigrationExportService : IMigrationExportService
	{
		public Message GetSecurityDefinitions()
		{
			List<MigrationRole> output = new List<MigrationRole>();

			IDictionary<string, SecurityProvider> providers = SecurityManager.Providers;

			foreach (KeyValuePair<string, SecurityProvider> provider in providers)
			{
				string providerName = provider.Key;

				SecurityManager securityManager = new SecurityManager(providerName);
			}

			MessageVersion ver = OperationContext.Current.IncomingMessageVersion;
			return Message.CreateMessage(ver, "GetSecurityDefinitionsResponse", output);
		}

		public Message GetUsersList()
		{
			List<MigrationItemContentDefinition> output = new List<MigrationItemContentDefinition>();

			MembershipProviderCollection providers = Membership.Providers;

			foreach (MembershipProvider provider in providers)
			{
				string providerName = provider.Name;
				UserManager userManager = new UserManager(providerName);

				int outRows;
				MembershipUserCollection users = new MembershipUserCollection();
				try
				{
					users = userManager.GetAllUsers(0, 0, "Email ASC", out outRows);
				}
				catch (Exception) { }

				foreach (MembershipUser user in users)
				{
					MigrationItemContentDefinition micd = new MigrationItemContentDefinition();

					micd.Name = user.UserName;
					micd.ProviderName = providerName;

					output.Add(micd);
				}
			}

			MessageVersion ver = OperationContext.Current.IncomingMessageVersion;
			return Message.CreateMessage(ver, "GetUsersListResponse", output);
		}

		public Message GetSingleUser(Message userDefinitionParam)
		{
			MigrationItemContentDefinition param = userDefinitionParam.GetBody<MigrationItemContentDefinition>();

			UserManager userManager = new UserManager(param.ProviderName);
			var user = userManager.GetUser(param.Name);
			var profiles = ProfileManager.FindProfilesByUserName(ProfileAuthenticationOption.Authenticated, user.UserName);

			MigrationUser result = new MigrationUser();

			result.ApplicationName = userManager.MembershipProvider.ApplicationName;
			result.Comment = user.Comment;
			result.CreationDate = user.CreationDate;
			result.Email = user.Email;
			result.IsApproved = user.IsApproved;
			result.IsLockedOut = user.IsLockedOut;
			result.ProviderName = param.ProviderName;
			result.ProviderUserKey = user.ProviderUserKey.ToString();
			result.Username = user.UserName;
			result.Password = null;

			if (userManager.MembershipProvider.EnablePasswordRetrieval &&
				userManager.MembershipProvider.PasswordFormat == MembershipPasswordFormat.Clear &&
				!userManager.MembershipProvider.RequiresQuestionAndAnswer)
			{
				try
				{
					result.Password = user.GetPassword();
				}
				catch { }
			}
			
			string[] roles = Roles.GetRolesForUser(user.UserName);
			for (int i = 0; i < roles.Length; i++)
			{
				result.Roles.Add(roles[i]);
			}

			try
			{
				result.LastActivityDate = user.LastActivityDate.ToUniversalTime();
			}
			catch (NotSupportedException)
			{
				result.LastActivityDate = DateTime.MinValue;
			}

			try
			{
				result.LastLockoutDate = user.LastLockoutDate.ToUniversalTime();
			}
			catch (NotSupportedException)
			{
				result.LastActivityDate = DateTime.MinValue;
			}

			try
			{
				result.LastLoginDate = user.LastLoginDate.ToUniversalTime();
			}
			catch (NotSupportedException)
			{
				result.LastActivityDate = DateTime.MinValue;
			}

			try
			{
				result.LastPasswordChangedDate = user.LastPasswordChangedDate.ToUniversalTime();
			}
			catch (NotSupportedException)
			{
				result.LastActivityDate = DateTime.MinValue;
			}

			try
			{
				result.PasswordQuestion = user.PasswordQuestion;
			}
			catch (NotSupportedException)
			{
				result.LastActivityDate = DateTime.MinValue;
			}

			var ver = OperationContext.Current.IncomingMessageVersion;
			return Message.CreateMessage(ver, "GetSingleUserResponse", result);
		}
	}
}
